package com.cyh.forum.controller;


import com.cyh.forum.common.AppResult;
import com.cyh.forum.common.ResultCode;
import com.cyh.forum.exception.ApplicationException;
import com.cyh.forum.model.Board;
import com.cyh.forum.services.IBoradService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Api(tags = "版块接口")
@RestController
@RequestMapping("/board")
public class BoradController {

    @Value("${bit-forum.index.board-num:9}")    //  :9 表示如果配置文件没有指定数值,则使用默认值9
    private Integer indexBoardNum;

    @Resource
    private IBoradService iBoradService;

    /**
     *      查询 首页的版块列表
     * @return
     */
    @ApiOperation("主页中显示的版块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        List<Board> result = iBoradService.selectByNum(indexBoardNum);
        return AppResult.success(result);
    }


    /**
     *  获取版块详情
     * @param id    版块id
     * @return  版块详情
     */
    @ApiOperation("获取版块详情接口")
    @GetMapping("/getById")
    public AppResult<Board> getBoardInfo(@ApiParam("版块id") @RequestParam("id") @NonNull Long id){
        Board board = iBoradService.selectByPrimaryKey(id);
        //  校验获取的的版块对象 是否存在
        if (board == null ||board.getDeleteState() == 1){
            //  打印日志
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            //  抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_BANNED));
        }

        return AppResult.success(board);
    }

}
